/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.model.dao.mem;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.sql.Date;
import java.util.Collection;

import net.groupme.exceptions.DeleteFailedException;
import net.groupme.exceptions.InsertFailedException;
import net.groupme.exceptions.UpdateFailedException;
import net.groupme.gui.constants.VIEW_VOCABULARY;
import net.groupme.model.Group;
import net.groupme.model.Resource;
import net.groupme.model.Tag;
import net.groupme.model.User;
import net.groupme.model.dao.ResourceDAO;
import net.groupme.model.dao.TagDAO;

/**
 * created on Oct 24, 2007
 * 
 * This class is an In-Memory-DAO implementation of a {@link TagDAO}. Hence, nothing is
 * stored within a database. <b>ATTENTION:</b> Functions that require additional
 * infos like {@link Tag#getResources(User)} are not implemented!!
 * 
 * Only setters and getters of id, keyword, and description are working.
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.5 $ $Date: 2009-03-11 23:13:35 $
 */
public class TagDAOMemImpl implements TagDAO {
	
	private String id = null;
	private String keyword = null;
	private String description = null;

	/**
	 * Creates a new TagDAOMemImpl instance.
	 * @param id the id of the tag
	 * @param keyword the actual tag
	 * @param description a description (optional)
	 */
	public TagDAOMemImpl(String id, String keyword, String description) {
		super();
		this.id = id;
		this.keyword = keyword;
		this.description = description;
	}
	
	/**
	 * DUMMY CONSTRUCTOR!
	 * Creates a new TagDAOMemImpl instance. The given keyword will be used as ID and the descriptiuon will be <code>null</code>.
	 * 
	 * @param keyword the actual tag
	 * 
	 */
	public TagDAOMemImpl(String keyword) {
		super();
		this.id = keyword;
		this.keyword = keyword;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Tag#add(net.groupme.model.Resource, net.groupme.model.User, net.groupme.model.Group, java.sql.Date, double)
	 */
	public void add(Resource resource, User user, Group group, Date date,
			double importance) throws InsertFailedException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Tag#getKeyword()
	 */
	public String getKeyword() {
		return this.keyword;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Tag#getResources()
	 */
	public Collection<Resource> getResources() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Tag#getResources(net.groupme.model.User)
	 */
	public Collection<Resource> getResources(User tagger) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Tag#getResources(net.groupme.model.User, net.groupme.model.Group)
	 */
	public Collection<Resource> getResources(User user, Group group) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Tag#getUsers(net.groupme.model.Resource)
	 */
	public Collection<User> getUsers(Resource resource) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Tag#remove(net.groupme.model.Resource, net.groupme.model.User, net.groupme.model.Group)
	 */
	public void remove(Resource resource, User user, Group group)
			throws DeleteFailedException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Tag#setKeyword(java.lang.String)
	 */
	public void setKeyword(String keyword) throws UpdateFailedException {
		this.keyword = keyword;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#getDescription()
	 */
	public String getDescription() {
		return this.description;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#getId()
	 */
	public String getId() {
		return this.id;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#getLabel()
	 */
	public String getLabel() {
		return this.keyword;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#getURI()
	 */
	public URI getURI() {
		if(keyword == null){
			keyword = getKeyword();
		}
		try {
			String URLkeyword = keyword.replaceAll(" ", "_");	
			URLkeyword = URLEncoder.encode(URLkeyword, "UTF-8");	
			return new URI(VIEW_VOCABULARY.RESTful_TAG_URI_PATTERN + URLkeyword);//$NON-NLS-1$
		} catch (URISyntaxException e) {
			e.printStackTrace();
			return null;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#setDescription(java.lang.String)
	 */
	public void setDescription(String description) throws UpdateFailedException {
		this.description = description;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#setLabel(java.lang.String)
	 */
	public void setLabel(String label) {
		this.keyword = label;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object otherTag) {
		if(otherTag instanceof Tag){
			return this.id.equals(((Tag)otherTag).getId());
		}
		return false;
	}

}
